﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpBasic.ArrayTest
{
    /// <summary>
    /// 数组测试1
    /// </summary>
    public class ArrayTest1
    {
        public static void DoTest()
        {

            //Test1();

            Test2();
        }


        /// <summary>
        /// Array 是所有数组的基类
        /// Array 类提供对数组操作的属性和方法。
        /// </summary>
        public static void Test1()
        {
            //数组定义方法1
            string[] arrInfo = new string[] { "a", "b", "d", "k","e" };

            //数组定义方法2
            string[] arrInfo2 = { "a", "b", "d", "k", "e" };

            //数组定义方法3
            string[] arrInfo3 = new string[5];
            arrInfo3[0] = "a";
            arrInfo3[1] = "b";
            arrInfo3[2] = "d";
            arrInfo3[3] = "k";
            arrInfo3[4] = "e";

            //原数组排序
            foreach (string s in arrInfo)
            {
                Console.WriteLine(s);
            }

            //数组倒排
            Array.Reverse(arrInfo);
            foreach (string s in arrInfo)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("数组排序");

            Array.Sort(arrInfo);
            foreach (string s in arrInfo)
            {
                Console.WriteLine(s);
            }

            int[] arrValue = { 10, 5, 20, 8 };
            Array.Sort(arrValue);
            foreach (int i in arrValue)
            {
                Console.WriteLine(i);
            }


        }

        /// <summary>
        /// 将数组使用"|" 将字符串数组使用进行合并字符串
        /// </summary>
        public static void Test2()
        {
            string[] arrInfo = { "a", "b", "c" };

            string result = JoinArray("|", arrInfo);
            Console.WriteLine(result);

            string result2 = JoinArray(">", arrInfo);
            Console.WriteLine(result2);

            string result3 = JoinArray(">>>>", arrInfo);
            Console.WriteLine(result3);
        }


        /// <summary>
        /// 将一个数组使用指定字符串将数组值分隔转换成字符串
        /// </summary>
        /// <param name="glue"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        public static string JoinArray(string glue, IEnumerable array)
        {
            var result = new List<string>();

            foreach (var item in array)
            {
                result.Add(item.ToString());
            }

            return string.Join(glue, result);
        }
    }
}
